package com.pxmeta.app.handler;

import cn.dev33.satoken.stp.StpInterface;
import com.google.common.collect.Lists;
import com.pxmeta.circle.entity.CirclePrivilege;
import com.pxmeta.circle.entity.UserCircle;
import com.pxmeta.circle.service.ICirclePrivilegeService;
import com.pxmeta.circle.service.IUserCircleService;
import com.pxmeta.common.enums.CircleUserLevelEnum;
import com.pxmeta.common.utils.AuthUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
public class StpHandler implements StpInterface {
    private final ICirclePrivilegeService circlePrivilegeService;
    private final IUserCircleService userCircleService;


    /**
     * 获取用户权限
     * @param loginId
     * @param loginType
     * @return
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        Long userId = Long.valueOf(String.valueOf(loginId));
        List<UserCircle> userCircles =  userCircleService.listByUserId(userId);
        List<String> permission = Lists.newLinkedList();
        //用户的所有圈子
        List<Long> circleIds = userCircles.stream().map(UserCircle::getCircleId).collect(Collectors.toList());
        List<CirclePrivilege> circlePrivileges = circlePrivilegeService.listByCircleId(circleIds);
        for (UserCircle userCircle : userCircles) {
            List<String> permissionCodes = Lists.newLinkedList();
            Integer level = userCircle.getLevel();
            Long circleId = userCircle.getCircleId();
            //管理员，查询管理员权限
            if (CircleUserLevelEnum.ADMIN.getLevel().equals(level)){
                //用户权限code
                List<String> codeList = circlePrivileges.stream()
                        .filter(cp -> circleId.equals(cp.getCircleId())
                                &&cp.getUserType() == 1  //用户是管理
                                && cp.getStatus() == 1) //允许权限
                        .map(CirclePrivilege::getCode).collect(Collectors.toList());
                for (String code : codeList) {
                    String finalCode = AuthUtil.getCode(circleId, code);
                    permissionCodes.add(finalCode);
                }
            }
            //圈子
            if (CircleUserLevelEnum.CREATOR.getLevel().equals(level)){
                String finalCode = AuthUtil.getCode(circleId, "*"); //圈主拥有所有权限
                permissionCodes.add(finalCode);
            }
            //普通用户
            if (CircleUserLevelEnum.NORMAL.getLevel().equals(level)){
                //用户权限code
                List<String> codeList = circlePrivileges.stream()
                        .filter(cp -> circleId.equals(cp.getCircleId())
                                &&cp.getUserType() == 2  //用户是普通成员
                                && cp.getStatus() == 1) //允许权限
                        .map(CirclePrivilege::getCode).collect(Collectors.toList());

                for (String code : codeList) {
                    String finalCode = AuthUtil.getCode(circleId, code);
                    permissionCodes.add(finalCode);
                }
            }
            permission.addAll(permissionCodes);
        }

        return permission;
    }





    /**
     * 获取用户角色
     * @param loginId
     * @param loginType
     * @return
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        Long userId = Long.valueOf(String.valueOf(loginId));
        List<UserCircle> userCircles = userCircleService.listByUserId(userId);
        List<String> roleList = Lists.newLinkedList();
        for (UserCircle userCircle : userCircles) {
            Long circleId = userCircle.getCircleId();
            Integer level = userCircle.getLevel();
            String role = AuthUtil.getRole(circleId, level);
            roleList.add(role);
        }
        return roleList;
    }
}
